package com.sgb.member.view_model;

import android.text.TextUtils;

import com.sgb.member.MemberConfig;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ApprovalRecordBean;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberAuditBean;
import com.sgb.member.entity.QualityMemberBean;
import com.sgb.member.ui.fragment.MemberInviteOrApplyApprovalFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.StringUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MemberInviteOrApplyApprovalModel extends BaseViewModel {

    MemberInviteOrApplyApprovalFragment fragment;
    MemberRepertory repertory;
    int current;

    public MemberInviteOrApplyApprovalModel(MemberInviteOrApplyApprovalFragment fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
        }
    }

    /**
     * 获取企业列表
     *
     * @param curPageType 0 邀请会员；1申请会员
     * @param isLoadMore  是否加载更多
     */
    public void getInviteOrApplyCompanyList(int curPageType, boolean isLoadMore, String text, String approvalNo, String areaNo) {
        showOrHideDialog(true);
        int currentPage = isLoadMore ? current + 1 : 1;
        Map<String, Object> map = new HashMap<>();
        map.put("limit", 20);
        map.put("page", currentPage);
        map.put("businessName", StringUtil.isEmpty(text) ? "" : text);
        switch (curPageType) {
            case 0:
                map.put("type", "2"); //审批类型 1申请 2邀请 3解除
                break;
            case 1:
                map.put("type", "1"); //审批类型 1申请 2邀请 3解除
                break;
        }
        map.put("status", approvalNo); // 	1审批中2通过3拒绝
        map.put("provinceNo", areaNo); // 加盟企业省份
        map.put("memberType", ""); // 会员主体:企业1个人2
        repertory.getApi().getMixMemberApprovalListByPage(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<ApprovalRecordBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<ApprovalRecordBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }
        });
    }

    /**
     * 获取审批流程状态
     * <p>
     * 邀请加盟审批类型 inviteJoinCompany
     * 申請加盟审批类型 applyJoinCompany
     * 会员企业调级 memberEvaluate
     * 会员解除申请 memberRelieve
     */
    public void getProcessStatus(String companyNo, String approveType) {
        showOrHideDialog(true);
        repertory.getApi().getProcessStatus(companyNo, approveType).enqueue(new ICallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                // "status": "1" //1 表示有审批 0 代表无审批
                String status = ((Map<String, String>) data.getData()).get("status");
                if (!TextUtils.isEmpty(status)) {
                    fragment.updateProcessStatus(status);
                } else {
                    MToast.showToast(fragment.getActivity(), "网络异常，请稍后重试");
                }
            }
        });
    }

    public void getBusinessTypes(int type) {
        showOrHideDialog(true);
        repertory.getApi().getServiceTypeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setChooseItemData(type, data.getData());
                }
            }
        });
    }

    /**
     * 获取省市区地址
     */
    public void getAddressModel(int type) {
        showOrHideDialog(true);
        repertory.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                showOrHideDialog(false);
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : data.getData()) {
                        entities.add(new CommonPopEntity(datum.getNo(), datum.getSimpleName()));
                    }
                    entities.add(0, new CommonPopEntity("", "全国"));
                    fragment.setChooseItemData(type, entities);
                }
            }
        });
    }

    public void getInviteOrApplyStatus(int type) {
        // 审批状态：审批中、待审批、已拒绝 1待审批，2审批通过 3，拒绝
        showOrHideDialog(true);
        repertory.getApi().getApproveStatus().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setChooseItemData(type, data.getData());
                }
            }
        });
    }

    public void getUrlSetting(ApprovalRecordBean memberBean, String param) {
        showOrHideDialog(true);
        repertory.getApi().getUrlSetting(param).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + "?no=" + memberBean.getNo()
                        + "&auditId=" + memberBean.getProcessId()+"&status="+memberBean.getStatus();
                MemberConfig.getInstance().startWebViewX5(fragment.getContext(), url, 101);
            }
        });
    }
}
